home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / COOLVIEW.ZIP / BIN2INC.PAS < prev    next >
Pascal/Delphi Source File  |  1995-03-05  |  6KB  |  216 lines

  1. {$I-}
  2. { coded by Paradise - use /help for more info }
  3. uses Crt,Dos,BinHex;
  4.  
  5. const
  6.   MaxLen = 60;
  7.  
  8. var
  9.   { variables }
  10.   input_name, output_name  : string;
  11.   input_file               : file;
  12.   output_file              : text;
  13.   fsize, counter           : longint;
  14.   b                        : byte;
  15.   w                        : word;
  16.   line,name                : string;
  17.   vstr                     : string;
  18.   { options }
  19.   wordmode, binmode,
  20.   hexmode, decmode,
  21.   asciimode                : boolean;
  22.  
  23. procedure Pause;
  24. begin
  25.   writeln('Pause (Press any key to continue)...');
  26.   readkey;
  27.   while keypressed do readkey;
  28.   writeln;
  29. end;
  30.  
  31. procedure ShowHelp;
  32. begin
  33.   writeln;
  34.   writeln('- -- ──- ─ HeLP 4 BiN2iNC -──  ─ -- ─');
  35.   writeln('- 1994,III.3, Lublin, Poland, Earth, Milky Way, Sun System.... ');
  36.   writeln;
  37.   writeln('This util has been coded by Paradise (a.k.a Marcin Jaskowiak),');
  38.   writeln('to help you with converting binary files to assembler data inc.');
  39.   writeln('Here comes the usage...');
  40.   writeln;
  41.   writeln('Options:             Info: ');
  42.   writeln('   /help              - show this help ');
  43.   writeln('   /word              - turn on word mode (standard is byte) ');
  44.   writeln('   /hex               - turn on hex mode (standard is dec) ');
  45.   writeln('                        example: the "db 68" will be replaced by ');
  46.   writeln('                        "db 044h" etc, etc.. ');
  47.   writeln('   /bin               - turn on binary mode, same as /hex but ');
  48.   writeln('                        for example: "db 3" will be replaced by ');
  49.   writeln('                        "db 00000011b" or "db 0000000000000011b" ');
  50.   writeln('                        in word mode ');
  51.   writeln('   /ascii             - same as /hex & /bin but byte for ex. "db 65" ');
  52.   writeln('                        will be replace by "db ''A''" ');
  53.   writeln;
  54.   pause;
  55.   writeln('If you have some problems or comments you can mail me to : ');
  56.   writeln(' paradise@bachus.umcs.lublin.pl or ');
  57.   writeln(' paradise@ajax.umcs.lublin.pl or ');
  58.   writeln(' paradise@anon.penet.fi ');
  59.   writeln;
  60.   writeln('You can also catch me on IRC on #coders or *private* :) or chat ');
  61.   writeln('me on mpoli bbs. ');
  62.   writeln;
  63.   writeln('My snail-mail address is : ');
  64.   writeln(' Marcin Jaskowiak ');
  65.   writeln(' Ul.Zarnowiecka 3/114 ');
  66.   writeln(' 20-630 Lublin, Poland ');
  67.   writeln;
  68.   writeln('That''s all folks!');
  69.   Halt(0);
  70. end;
  71.  
  72. procedure ParseOptions;
  73. var
  74.   cnt : byte;
  75.   pst : string;
  76. begin
  77.   { default options }
  78.   wordmode:=false;
  79.   binmode:=false;
  80.   hexmode:=false;
  81.   decmode:=true;
  82.   asciimode:=false;
  83.   { read parameters }
  84.   for cnt:=1 to paramcount do
  85.   begin
  86.     pst:=paramstr(cnt);
  87.     if pst='/word' then wordmode:=true else
  88.     if (pst='/help') or (pst='/h') or (pst='/?') then ShowHelp else
  89.     if pst='/hex' then
  90.     begin
  91.       hexmode:=true;
  92.       decmode:=false;
  93.       binmode:=false;
  94.       asciimode:=false;
  95.     end else
  96.     if pst='/ascii' then
  97.     begin
  98.       asciimode:=true;
  99.       decmode:=false;
  100.       binmode:=false;
  101.       hexmode:=false;
  102.     end else
  103.     if pst='/bin' then
  104.     begin
  105.       binmode:=true;
  106.       decmode:=false;
  107.       hexmode:=false;
  108.       asciimode:=false;
  109.     end else
  110.     begin
  111.      if cnt=1 then input_name:=pst;
  112.      if cnt=2 then output_name:=pst;
  113.     end;
  114.   end;
  115. end;
  116.  
  117. procedure Ask(title : string; var result : string);
  118. begin
  119.   write(title);
  120.   readln(result);
  121. end;
  122.  
  123. function NoFile(fname : string): boolean;
  124. var
  125.   okie : boolean;
  126.   ff   : file;
  127. begin
  128.   assign(ff,fname);
  129.   reset(ff,1);
  130.   okie:=(IOResult=0);
  131.   if okie then close(ff);
  132.   NoFile:=not okie;
  133. end;
  134.  
  135. procedure Error(title : string);
  136. begin
  137.   writeln('Error! ',title,'.');
  138.   writeln('Abnormal program termination.');
  139.   halt(1);
  140. end;
  141.  
  142. procedure Warning(title: string);
  143. begin
  144.   writeln('Warning! ',title,'.');
  145. end;
  146.  
  147. procedure Convert;
  148. begin
  149.  if asciimode and wordmode then Error('Cannot use word and ascii mode together');
  150.  writeln('Converting "',input_name,'" to "',output_name,'".');
  151.  assign(input_file,input_name);
  152.  assign(output_file,output_name);
  153.  reset(input_file,1);
  154.  rewrite(output_file);
  155.  fsize:=filesize(input_file);
  156.  if (fsize mod 2<>0) and wordmode then
  157.  begin
  158.    dec(fsize);
  159.    Warning('File size fail, size truncated');
  160.  end;
  161.  if wordmode then fsize:=fsize div 2;
  162.  line:='     db ';
  163.  for counter :=1 to fsize do
  164.  begin
  165.    if wordmode then blockread(input_file,w,2)
  166.                else blockread(input_file,b,1);
  167.    if hexmode  then
  168.    begin
  169.      if wordmode then vstr:=Word2Hex(w)
  170.                  else vstr:=Byte2Hex(b);
  171.    end;
  172.    if binmode  then
  173.    begin
  174.      if wordmode then vstr:=Word2Bin(w)
  175.                  else vstr:=Byte2Bin(b);
  176.    end;
  177.    if decmode  then
  178.    begin
  179.      if wordmode then vstr:=Word2Dec(w)
  180.                  else vstr:=Byte2Dec(b);
  181.    end;
  182.    if asciimode  then
  183.    begin
  184.      vstr:=Byte2Asc(b);
  185.    end;
  186.    line:=line+vstr;
  187.    if length(line)>MaxLen then
  188.    begin
  189.      writeln(output_file,line);
  190.      line:='     db ';
  191.    end else line:=line+',';
  192.  end;
  193.  if line[length(line)]=',' then line[length(line)]:=' ';
  194.  if line<>'     db ' then writeln(output_file,line);
  195.  close(input_file);
  196.  close(output_file);
  197. end;
  198.  
  199. begin
  200.   writeln;
  201.   writeln('BiN2iNC version 1.1 (c) 1995 by Paradise');
  202.   writeln('Use param "/help" for help and info.');
  203.   ParseOptions;
  204.   if input_name='' then Ask('Enter input name : ',input_name);
  205.   if input_name='' then Error('Interrupted by user');
  206.   if pos('.',input_name)=0 then input_name:=input_name+'.';
  207.   if NoFile(input_name) then Error('Cannot find input file');
  208.   name:=copy(input_name,1,pos('.',input_name))+'inc';
  209.   if output_name='' then Ask('Enter output name ('+name+') : ',output_name);
  210.   if output_name='' then output_name:=name;
  211.   Convert;
  212.   writeln('That''s all folks!');
  213. end.
  214.  
  215.  
  216.